package com.sanjin.mall.product.service.impl;

import com.sanjin.mall.common.constant.CategoryConstant;
import com.sanjin.mall.product.service.CategoryBrandRelationService;
import com.sanjin.mall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanjin.mall.common.utils.PageUtils;
import com.sanjin.mall.common.utils.Query;

import com.sanjin.mall.product.dao.CategoryDao;
import com.sanjin.mall.product.entity.CategoryEntity;
import com.sanjin.mall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelService;

    @Override
    public List<CategoryEntity> getCategoryEntityTree() {
        List<CategoryEntity> tree = null;

        List<CategoryEntity> list = baseMapper.selectList(new QueryWrapper<>());
        tree = list.stream()
                .peek(a -> a.setChildren(getSelfChildren(a, list)))
                .filter(a->a.getParentCid() == 0)
                .collect(Collectors.toList());

        return tree;
    }

    private List<CategoryEntity> getSelfChildren(CategoryEntity a, List<CategoryEntity> list) {
        List<CategoryEntity> children = null;

        children = list.stream()
                .filter(x -> x.getParentCid() == a.getCatId())
                .collect(Collectors.toList());

        //# TODO 检查逻辑
        return children.size() == 0 ? null : children;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void removeMenuByIds(List<Long> catIds) {
        //TODO 1.检查当前删除的菜单，是否被别的地方引用
        //其实开发中使用的都是逻辑删除，并不是真正物理意义上的删除
        baseMapper.deleteBatchIds(catIds);
    }

    //查询三级分类的三个id
    @Override
    public Long[] getCatelogPath(Long cateId) {
        Long[] idPath = new Long[3];
        long id = cateId;
        idPath[2] = id;
        idPath[1] = getById(cateId).getParentCid();
        idPath[0] = getById(idPath[1]).getParentCid();
        return idPath;
    }
    //级联更新 cate和brand关联表
    @Override
    public void updateDetail(CategoryEntity category) {
        updateById(category);
        categoryBrandRelService.updateCatelogNameById(category);
    }

    @Override
    public List<CategoryEntity> getLevel_1() {
        return this.list(new QueryWrapper<CategoryEntity>()
                .eq(CategoryConstant.level_field, CategoryConstant.level_1));
    }

    @Override
    public Map<String, List<Catelog2Vo>> getCatalogJson() {

        //1.查出所有1级分类
        List<CategoryEntity> level1Categorys = getLevel_1();

        //2.封装数据
        Map<String, List<Catelog2Vo>> parent_cid = level1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
            //1.每一个的一级分类，查到这个一级分类的二级分类
            List<CategoryEntity> categoryEntities = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", v.getParentCid()));
            //2.封装上面的结果
            List<Catelog2Vo> catelog2Vos = null;
            if (categoryEntities != null) {
                catelog2Vos = categoryEntities.stream().map(l2 -> {
                    Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), null, l2.getCatId().toString(), l2.getName());
                    //1.找到当前二级分类的三级分类,封装成 vo
                    List<CategoryEntity> level3Catelog = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", l2.getCatId()));
                    // 三级分类有数据的情况下
                    if (level3Catelog != null){
                        List<Catelog2Vo.Catelog3Vo> collect = level3Catelog.stream().map(l3 -> {
                            //2.封装成指定格式
                            Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo(l2.getCatId().toString(), l3.getCatId().toString(), l3.getName());
                            return catelog3Vo;
                        }).collect(Collectors.toList());
                        catelog2Vo.setCatalog3List(collect);
                    }
                    return catelog2Vo;

                }).collect(Collectors.toList());

            }
            return catelog2Vos;
        }));

        return parent_cid;
    }


}